package com.hollycrm.hollysqm.core.waveform.plot;


import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import com.hollycrm.hollysqm.core.waveform.plot.model.DrawableObject;


/***
 * UI方法，提供图形绘制接口
 *
 * @author RobinTang
 */

final public class Plot {
    private List<PlotFrame> plotFrameStack = null;
    private PlotFrame currenPlotFrame = null;
    private int plotFrameConter = 1;
    private boolean autoFigure = false; // 自动创建图形窗口


    /**
     * 清空内存
     */
    public void clear() {
        plotFrameStack = null;
        currenPlotFrame = null;
        plotFrameConter = 1;
        autoFigure = false; // 自动创建图形窗口
    }


    /**
     * 初始化图形窗口栈
     */
    private void initPlotFrameStack() {
        if (plotFrameStack == null) {
            plotFrameStack = new ArrayList<PlotFrame>();
        }
    }

    /**
     * 初始化当前图形窗口
     */
    private void initCurrentPlotFrame() {
        if (currenPlotFrame == null) {
            figrue();
        }
    }

    // like matlab

    /**
     * 开图形保存
     */
    public void hold_on() {
        initCurrentPlotFrame();
        currenPlotFrame.setHoldOn(true);
    }

    /**
     * 关图形保持
     */
    public void hold_off() {
        initCurrentPlotFrame();
        currenPlotFrame.setHoldOn(false);
    }

    /**
     * 开自动创建图形窗口
     */
    public void autofigure_on() {
        autoFigure = true;
    }

    /**
     * 关自动创建图形窗口
     */
    public void autofigure_off() {
        autoFigure = false;
    }

    /**
     * 打开图形窗口
     */
    public PlotFrame figrue() {
        return figrue(plotFrameConter);
    }

    public PlotFrame figrue(String title) {
        initPlotFrameStack();
        currenPlotFrame = null;
        for (PlotFrame fm : plotFrameStack) {
            if (title.equals(fm.getTitle())) {
                currenPlotFrame = fm;
                break;
            }
        }
        if (currenPlotFrame == null)
            addPlotFrame(new PlotFrame(title, Constant.iv));
        return currenPlotFrame;
    }

    public PlotFrame figrue(int index) {
        return figrue("Figure " + index);
    }

    /**
     * 绘图
     */
    public PlotFrame plot(DrawableObject drawableObject) {

        if (autoFigure)
            figrue();
        else
            initCurrentPlotFrame();
        currenPlotFrame.plot(drawableObject);
        suit();
        return currenPlotFrame;
    }

    /**
     * 绘图生成图片
     */
    public PlotFrame plot(DrawableObject drawableObject,
                          String fileName, int w, int h, Color backgroundColor) {
        if (autoFigure)
            figrue();
        else
            initCurrentPlotFrame();
        currenPlotFrame.plot(drawableObject);
        suit();
        //绘制图片
        PlotFrame p = plotFrameStack.get(0);
        p.getPlotPanle().paintImage(fileName, w, h, backgroundColor);

        return currenPlotFrame;
    }

    public PlotFrame plot(double[] y) {
        return plot(new DrawableObject(y));
    }

    public PlotFrame plot(double[] x, double[] y) {
        return plot(new DrawableObject(x, y));
    }

    public PlotFrame plot(double[][] pts) {
        return plot(new DrawableObject(pts[0], pts[1]));
    }

    public PlotFrame plot(double[] x, String pams) {
        return plot(new DrawableObject(x, pams));
    }

    public PlotFrame plot(double[] x, String pams, String fileName, int w, int h, Color backgroundColor) {
        return plot(new DrawableObject(x, pams), fileName, w, h, backgroundColor);
    }


    public PlotFrame plot(double[] x, double[] y, String pams) {
        return plot(new DrawableObject(x, y, pams));
    }

    public PlotFrame plot(double[][] pts, String pams) {
        return plot(new DrawableObject(pts[0], pts[1], pams));
    }

    /**
     * 设置显示范围
     */
    public void axis(double sx, double ex, double sy, double ey) {
        initCurrentPlotFrame();
        currenPlotFrame.axis(sx, ex, sy, ey);
    }

    /**
     * 最佳视野
     */
    public void suit() {
        initCurrentPlotFrame();
        currenPlotFrame.suit();
    }

    // internal
    public void addPlotFrame(PlotFrame frame) {
        initPlotFrameStack();
        ++plotFrameConter;
        plotFrameStack.add(frame);
        currenPlotFrame = frame;
    }

    public boolean removePlotFrame(PlotFrame frame) {
        plotFrameStack.remove(frame);
        return plotFrameStack.size() != 0;
    }


}
